package com.fingard.dsp.bank.directbank.citic05;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.citic.openbank.sdk.CiticSdk;
import com.citic.openbank.sdk.CiticSdkInterface;
import com.citic.openbank.sdk.RequestHeader;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;
import com.fingard.text.StringHelper;

/**
 * 中信收付通_开放银行签约
 */
public class CITIC05Contract extends CITIC05Base implements IContract {

    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_req.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail(p_req);
        String ownAct = p_req.contractDetail.recAct;
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(ownAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + ownAct + "]商户配置信息!";
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            CITIC05Header bankHeader = new CITIC05Header();
            CiticSdkInterface sdk = CiticSdk.getInstance();
            setKeys(sdk, actItem);
            setConfiguration(sdk, actItem);
            RequestHeader header = new RequestHeader();
            JSONObject jsonRequest = new JSONObject();
            if ("01".equals(p_req.contractDetail.step)) {
                bankHeader.openTransCode = "IFOPS013";
                bankHeader.openMerFlowId = p_req.contractDetail.askNumber;
                writeCommonHeader(header, bankHeader, actItem);
                jsonRequest.put("sndseqno", p_req.contractDetail.askNumber);
                jsonRequest.put("mccprtcno", actItem.recAgNum);
                jsonRequest.put("mrchname", actItem.userCode);
                jsonRequest.put("mrchtype", actItem.userFlag);
                jsonRequest.put("payname", p_req.contractDetail.payName);
                jsonRequest.put("payacc", p_req.contractDetail.payAct);
                jsonRequest.put("phone", p_req.contractDetail.cellPhone);
                jsonRequest.put("idtype", convertCertTypeToBank(p_req.contractDetail.certType));
                jsonRequest.put("idno", p_req.contractDetail.certNum);
                String deadLine = p_req.contractDetail.deadLine;
                if (StringHelper.isNullOrEmpty(deadLine)) {
                    deadLine = Format.Date10Format.format(getBankConfig().getMapValueByKey("contractKeyMapping", "deadLine"));
                    if (StringHelper.isNullOrEmpty(deadLine)) {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "缺失配置节点'deadLine'或值为空!";
                        retAts.writeRetDetail(tmpRetDetail);
                        return retAts;
                    }
                }
                jsonRequest.put("duedt", deadLine);
                String singleLimit = p_req.contractDetail.singleLimit;
                if (StringHelper.isNullOrEmpty(singleLimit)) {
                    singleLimit = getBankConfig().getMapValueByKey("contractKeyMapping", "singleLimit");
                    if (StringHelper.isNullOrEmpty(singleLimit)) {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "缺失配置节点'singleLimit'或值为空!";
                        retAts.writeRetDetail(tmpRetDetail);
                        return retAts;
                    }
                }
                jsonRequest.put("sigllimtamt", singleLimit);
                String dayAmtLimit = p_req.contractDetail.dayAmtLimit;
                if (StringHelper.isNullOrEmpty(dayAmtLimit)) {
                    dayAmtLimit = getBankConfig().getMapValueByKey("contractKeyMapping", "dayAmtLimit");
                    if (StringHelper.isNullOrEmpty(dayAmtLimit)) {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "缺失配置节点'dayAmtLimit'或值为空!";
                        retAts.writeRetDetail(tmpRetDetail);
                        return retAts;
                    }
                }
                jsonRequest.put("daylimtamt", dayAmtLimit);
                String monthAmtLimit = p_req.contractDetail.monthAmtLimit;
                if (StringHelper.isNullOrEmpty(monthAmtLimit)) {
                    monthAmtLimit = getBankConfig().getMapValueByKey("contractKeyMapping", "monthAmtLimit");
                    if (StringHelper.isNullOrEmpty(monthAmtLimit)) {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "缺失配置节点'monthAmtLimit'或值为空!";
                        retAts.writeRetDetail(tmpRetDetail);
                        return retAts;
                    }
                }
                jsonRequest.put("mothlimtamt", monthAmtLimit);

            } else if ("02".equals(p_req.contractDetail.step)) {
                bankHeader.openTransCode = "IFOPS014";
                bankHeader.openMerFlowId = p_req.contractDetail.askNumber;
                writeCommonHeader(header, bankHeader, actItem);
                jsonRequest.put("sndseqno", p_req.contractDetail.askNumber);
                jsonRequest.put("osndseqno", p_req.contractDetail.askNumber);
                jsonRequest.put("mccprtcno", actItem.recAgNum);
                jsonRequest.put("code", p_req.contractDetail.smsCode);
                jsonRequest.put("flag", "00");
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未能识别的step：" + p_req.contractDetail.step;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }

//            String tmpStrRet = sendToBank(header, jsonRequest.toJSONString());
            String tmpStrRet = sdk.remoteCall(header, jsonRequest.toJSONString());

            if (StringHelper.hasAnyChar(tmpStrRet)) {
                if (tmpStrRet.contains("{")) {
                    JSONObject tmpJsonResp = JSON.parseObject(tmpStrRet);
                    String retCode = tmpJsonResp.getString("RETCODE");
                    String retMsg = tmpJsonResp.getString("RETMSG");
                    if (StringHelper.hasAnyChar(retCode)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = retMsg;
                        if ("01".equals(p_req.contractDetail.step)) {
                            if (!"00".equals(retCode)) {
                                tmpRetDetail.respCode = "-1";
                            }
                            tmpRetDetail.signConfirmType = "2";
                        } else {
                            if ("05".equals(retCode)) {
                                tmpRetDetail.signState = ContractState.SUCCESS;
                            } else if ("00".equals(retCode)) {
                                tmpRetDetail.signState = ContractState.SIGNING;
                            } else {
                                tmpRetDetail.signState = ContractState.FAIL;
                            }
                        }
                        tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("contractMapping", retCode);
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = retMsg;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "银行响应数据格式非法";
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应为空";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }


    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_reqQry.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail(p_reqQry);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_reqQry.contractDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + p_reqQry.contractDetail.recAct + "]商户配置信息!";
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            CITIC05Header bankHeader = new CITIC05Header();
            bankHeader.openTransCode = "IFOPS009";
            bankHeader.openMerFlowId = p_reqQry.contractDetail.askNumber;
            CiticSdkInterface sdk = CiticSdk.getInstance();
            setKeys(sdk, actItem);
            setConfiguration(sdk, actItem);
            RequestHeader header = new RequestHeader();
            writeCommonHeader(header, bankHeader, actItem);
            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("sndseqno", p_reqQry.contractDetail.askNumber);
            jsonRequest.put("mccprtcno", actItem.recAgNum);
            jsonRequest.put("payname", p_reqQry.contractDetail.payName);
            jsonRequest.put("payacc", p_reqQry.contractDetail.payAct);
            jsonRequest.put("phone", p_reqQry.contractDetail.cellPhone);
            jsonRequest.put("idtype", convertCertTypeToBank(p_reqQry.contractDetail.certType));
            jsonRequest.put("idno", p_reqQry.contractDetail.certNum);

//            String tmpStrRet = sendToBank(header, jsonRequest.toJSONString());
            String tmpStrRet = sdk.remoteCall(header, jsonRequest.toJSONString());

            if (StringHelper.hasAnyChar(tmpStrRet)) {
                if (tmpStrRet.contains("{")) {
                    JSONObject tmpJsonResp = JSON.parseObject(tmpStrRet);
                    String retMsg = tmpJsonResp.getString("respinfo");
                    String stat = tmpJsonResp.getString("stat");
                    if (StringHelper.hasAnyChar(stat)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = retMsg;
                        if ("00".equals(stat)) {
                            tmpRetDetail.signState = ContractState.SUCCESS;
                            tmpRetDetail.authCode = tmpJsonResp.getString("protocolno");
                        } else if ("01".equals(stat)) {
                            tmpRetDetail.signState = ContractState.UNSIGNED;
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = retMsg;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = retMsg;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "银行响应数据格式非法";
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应为空";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}